<?php
include_once "models/report_model.php";
include_once('models/carProduct_model.php');
include_once('models/productfront_model.php');
include_once('models/alias_model.php');
include_once('controllers/frontend_controller.php');
include('components/imageSimple/SimpleImage.php');

class Productfront_Controller extends Frontend_Controller{
	public $model;
	public $action = 'lists';
	
	public function __construct() {
		parent::__construct();
		// Create Model class
		$this->model = new Productfront_Model();
		// Control action
		if(isset($_GET['action'])) $this->action = $_GET['action'];
		$this->{$this->action}();
	}

	public function lists() {
		$artPro =  $this->model->getListsPro();
		//$getProId =  $this->model->getIdCat();
		//var_dump($getProId); exit();
		//var_dump($artPro);exit();
		include 'views/product/product-lists.php';
	}
	public function getAllProForACat() {
		$cat=$_GET['name'];
		$CatPro =  $this->model->getAllProForACat();
		//$getProId =  $this->model->getIdCat();
		//var_dump($getProId); exit();
		//var_dump($artPro);exit();
		include 'views/product/listaCatforProductfront.php';
	}
	public function listsCat() {
	//if (isset($_GET['cat'])) exit('kunkun');
		$categories = $_GET['cat'];
		$artPro =  $this->model->getListsProFollowCat($categories);
		//var_dump($artPro); exit();
		
		//$getProId =  $this->model->getIdCat();
		//var_dump($getProId); exit();
		//var_dump($artPro);exit();
		include 'views/product/product-lists.php';
	}
	public function view() {
		//$id= $_GET['id'];
		//$artPro =  $this->model->getAPro($id);
		//$getProId =  $this->model->getIdCat();
		//var_dump($getProId); exit();
		//var_dump($artPro);exit();
		include 'views/product/product-lists.php';
	}
	public function beforeedit(){
		$id= $_GET['id'];
		$artPro =  $this->model->getAPro($id);
		$catModel = new CatProduct_Model();
		$idCat = $catModel->getListCate();
		//var_dump($idCat); exit();
		//$getProId =  $this->model->getIdCat();
		//var_dump($getProId); exit();
		//var_dump($artPro);exit();
		include 'views/product/EditProduct.php';
	}
	 //Method edit
    public  function edit(){
		
		// Intinial for Categories
		$catModel = new CatProduct_Model();
		$idCat = $catModel->getListCate();
		// End Intinial for Categories
		//if (isset($_POST['id']))  exit('fff');
        $id = $_POST['id'];
        $title = $_POST['title'];
		$createAlias= new Alias_Model();
		$alias= $createAlias->convert2Alias($title);
        $content = $_POST['content'];
		//$image=$_POST['image'];
		$idcat=$_POST['cat'];
		
        $this->model->editProduct($id,$title,$content,$alias);
        $this->model->deleteCatofProduct($id); //delete old Cat
		//Insert New cat
		$numberElement=sizeof($idcat);
		for ($i=0;$i<$numberElement;$i++)
			{
				$this->model->addCat($id,$idcat[$i]);
			}
        $artPro =  $this->model->getListsPro();
        include 'views/product/product-lists.php';
    }
	public function add(){
		$catModel = new CatProduct_Model();
		$idCat = $catModel->getListCate();
	//var_dump($_POST['btn_submit']);
	//exit();
		if(isset($_POST['btn_submit']))
		{
		//exit('kaka');
			if(!empty($_POST['title']) && !empty($_POST['content']) && $this->uploadImg($_FILES) && !empty($_POST['cat'])) 
			{ 	
				$img = $_FILES['image']['name'];
				$title = $_POST['title'];
				$createAlias= new Alias_Model();
				$alias= $createAlias->convert2Alias($title);
				$content = $_POST['content'];
				$idcat = $_POST['cat'];
				//count Categories of one product
				$numberElement=sizeof($idcat);
				//echo $t; exit();//
				$this->model->addPro($title,$content,$img,$alias);
				$IdProduct=$this->model->getId($title);
				$row = mysqli_fetch_array($IdProduct);
				for ($i=0;$i<$numberElement;$i++)
				{
				//var_dump($IdProduct);exit();
				$this->model->addCat($row['id'],$idcat[$i]);
				}
				//exit('sss');
				$artPro =  $this->model->getListsPro();
				//$idCate = $this->model->getIdCat($row['id']);
				//$getProId =  $this->model->getIdCat();
				//include 'views/product/product-lists.php';
				header('Location: index.php?controller=product&action=lists');
			}
			else
			{
				include 'views/product/addart.php';
			}
		}
		else
		{	
			include 'views/product/addart.php';
		}
	}
	public function delete(){
		$id = $_GET['id'];
		$img=$this->model->getImage($id);
		$row = mysqli_fetch_array($img);
		$buffer=$row['image'];
	//	exit($buffer);
		unlink("media/img/".$buffer);
		$this->model->deleteProduct($id);
		$this->model->deleteProductCat($id);
		$artPro = $this->model->getListsPro();
		$getProId =  $this->model->getIdCat();
        include'views/product/product-lists.php';
	}
	public function uploadImg($_FILES) {
		$t=time(); //get Time.
		$_FILES["image"]["name"]=$t.'.jpg';
		//exit($_FILES["image"]["name"]);
		//$_FILES["image"]["name"]->resize(250,400);
		$allowedExts = array("gif", "jpeg", "jpg", "png");
		$temp = explode(".", $_FILES["image"]["name"]);
		$extension = end($temp);
		if ((($_FILES["image"]["type"] == "image/gif")
		|| ($_FILES["image"]["type"] == "image/jpeg")
		|| ($_FILES["image"]["type"] == "image/jpg")
		|| ($_FILES["image"]["type"] == "image/pjpeg")
		|| ($_FILES["image"]["type"] == "image/x-png")
		|| ($_FILES["image"]["type"] == "image/png"))
		&& ($_FILES["image"]["size"] < 200000000)
		&& in_array($extension, $allowedExts))
		  {
		  if ($_FILES["image"]["error"] > 0)
		    {
		    echo 'error';
		    return false;
		    }
		    if (file_exists(SITE_URI."/media/img/" . $_FILES["image"]["name"]))
		      {
		      	return true;
		      }
		    else
		      {	
			 // $random_number = rand(1,10);
			//  $_TARGET=$_FILES["image"]["tmp_name"].$random_number . '.jpg';
			//  var_dump($_FILES["image"]["tmp_name"]); exit();
		        move_uploaded_file($_FILES["image"]["tmp_name"], "media/img/" . $_FILES["image"]["name"]);
				$image = new SimpleImage();
				$image->load("media/img/" . $_FILES["image"]["name"]);
				unlink("media/img/" . $_FILES["image"]["name"]);
				$image->resizeToWidth(250);
				$image->save("media/img/" . $_FILES["image"]["name"]);
		      return true;
		      }
		    }
		else
		  {
		  echo "Invalid file";
		  return false;
		  }
	}
}
?>